/*--------------------------------------------------------------------------------
 * Adaptive Intelligence - Windows Forms Controls Class Library
 * Version 2.00
 * 
 * Advanced List View Class
 *      Extends the standard ListView windows control that allows sorting of the 
 * contents by clicking the column headers.
 *------------------------------------------------------------------------------*/

/*--------------------------------------------------------------------------------
 * System Imports
 *------------------------------------------------------------------------------*/
using System;
using System.Text;
using System.ComponentModel;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Windows.Forms;

namespace Adaptive.Foundation.Windows.Forms
{
    /// <summary>
    /// Extends the standard <see cref="ListView"/> windows control that allows sorting of the 
    /// contents by clicking the column headers.
    /// </summary>
    [ToolboxBitmap(typeof(EmbeddedResourceFinder), "Adaptive.Foundation.Windows.Forms.AdvancedListView.bmp")]
    public partial class AdvancedListView : ListView
    {
        /*--------------------------------------------------------------------------------
         * Private Member Declarations
         *------------------------------------------------------------------------------*/
        #region Private Member Declarations
		/// <summary>
		/// The last column that was clicked.
		/// </summary>
		private int _lastColumnIndex = -1;
		/// <summary>
		/// The order in which the sorting is to take place.
		/// </summary>
		private bool _sortAscending = true;
        #endregion

        /*--------------------------------------------------------------------------------
         * Constructor / Dispose / Destructor Methods
         *------------------------------------------------------------------------------*/
        #region Constructor / Dispose / Destructor Methods
        /// <summary>
        /// Initializes a new instance of the class.
        /// </summary>
        /// <remarks>
        /// This is the default constructor.
        /// </remarks>
        public AdvancedListView()
            : base()
        {
            InitializeComponent();
        }
        /// <summary>
        /// Deallocates internally allocated objects.
        /// </summary>
        /// <param name="disposing">
        /// A value indicating whether the object is being disposed.
        /// </param>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (components != null)
                    components.Dispose();
            }
            components = null;
            base.Dispose(disposing);
        }
        #endregion

        /*--------------------------------------------------------------------------------
         * Protected Methods / Event Methods
         *------------------------------------------------------------------------------*/
        #region Protected Methods / Event Methods
        /// <summary>
        /// Performs sorting based on the column that was clicked, and raises the 
        /// <see cref="ListView.ColumnClick"/> event.
        /// </summary>
        /// <param name="e">
        /// A <see cref="ColumnClickEventArgs"/> object containing the index of the column that was clicked.
        /// </param>
        protected override void OnColumnClick(ColumnClickEventArgs e)
        {
            ListViewColumnSorter sorter = null;		//Comparer object.

            //Turn off auto-sort.
            Sorting = SortOrder.None;

            //If the same column is clicked more than once, reverse the sort order
            //each time; otherwise, sort ascending.
            if (e.Column == _lastColumnIndex)
                _sortAscending = !_sortAscending;
            else
                _sortAscending = true;

            //Save the last column that was clicked.
            _lastColumnIndex = e.Column;

            //Prepare to sort the contents.
            sorter = new ListViewColumnSorter(e.Column, _sortAscending);
            base.ListViewItemSorter = sorter;

            //Raise the base event.
            base.OnColumnClick(e);

            //Sort the contents.
            Sort();

            //Clear memory.
            sorter = null;
        }
        #endregion
    }
}
